﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class In_OutPersonInfoService : IIn_OutPersonInfoService
    {
        private IIn_OutPersonInfoDal _inpersionInfo;
        private IMapper _mapper;
        public In_OutPersonInfoService(IIn_OutPersonInfoDal inpersionInfo, IMapper mapper)
        {
            _inpersionInfo = inpersionInfo;
            _mapper = mapper;
        }

        public async Task<int> AddinOupAsync(string realName, int gender, string Tel, string address)
        {
            return await _inpersionInfo.AddAsync(new InOutpersoninfo 
            {
                RealName = realName,
                Gender = gender,
                Tel = Tel,
                Address = address
            });
        }

        public async  Task<int> DeleteinOupAsync(int id)
        {
            var data = await _inpersionInfo.QueryAsync(id);
            return data == null ? -1 : await _inpersionInfo.DeleteAsync(data);
        }

        public async  Task<int> EditinOupAsync(int id, string realName, int gender, string Tel, string address)
        {
            var data = await _inpersionInfo.QueryAsync(id);
            if (data == null)
                return -1;
            data.RealName = realName;
            data.Gender = gender;
            data.Tel = Tel;
            data.Address = address;
            return await _inpersionInfo.EditAsync(data);
        }

        public async  Task<List<In_OutPersonInfoDto>> GetinOupAllAsync()
        {
            var data = await _inpersionInfo.QueryAsync();
            return _mapper.Map<List<InOutpersoninfo>, List<In_OutPersonInfoDto>>(data);
        }

        public async  Task<In_OutPersonInfoDto> QueryinOupByIdAsync(int id)
        {
            var data = await _inpersionInfo.QueryAsync(id);
            return _mapper.Map<InOutpersoninfo, In_OutPersonInfoDto>(data);
        }

        public async  Task<List<In_OutPersonInfoDto>> QueryinOupByNameAsync(string realName)
        {
            var data = await _inpersionInfo.QueryAsync(inp => inp.RealName.Contains(realName));
            return _mapper.Map<List<InOutpersoninfo>, List<In_OutPersonInfoDto>>(data);
        }
    }
}
